﻿using System;
using System.Linq;
using System.Data;
using Newtonsoft.Json.Linq;

namespace TrinityAPI.Base.Tools
{
    public static class DataSetHelper
    {
        public static DataTable GetDataTable(this DataSet ds, int index = 0)
        {
            if (ds == null || index < 0)
                return null;

            if (ds.Tables.Count <= index)
                return null;

            return ds.Tables[index];
        }

        public static JArray GetJsonArray(this DataSet ds, int index = 0)
        {
            return JSONHelper.DataTableToJArray(ds.GetDataTable(index));
        }

        public static JObject GetJsonObject(this DataSet ds, int index = 0)
        {
            var array = ds.GetJsonArray(index);
            if (array != null && array.Count == 1)
                return array[0] as JObject;
            return null;
        }
    }
}